package com.ttdevs.spring.controller;

import com.ttdevs.spring.model.Operate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Created by ttdevs on 15/7/30.
 */

@Controller
@RequestMapping("/header")
public class HeaderController {

    @RequestMapping(value = "/param", method = RequestMethod.POST)
    public @ResponseBody String getHeader(@RequestHeader("User-Agent") String userAgent,
                      @RequestHeader("Content-Type") String contentType,
                      @RequestHeader("token") String token) {

        System.out.println(String.format(">>>>> %s >> %s >> %s", userAgent, contentType, token));

        return "{}";
    }

    @RequestMapping(value = "/{param}", method = {RequestMethod.GET})
    public
    @ResponseBody
    Object retrieve(@PathVariable() String param) {
        if (param.isEmpty()) {
            return "{\"error:\" : \"param is null\"}";
        }
        Operate operate = new Operate();
        operate.setMethod(RequestMethod.GET.name());
        operate.setParamter(param);
        operate.setRequest(null);
        operate.setResponse("retrieve objects with " + param);

        return operate;
    }

    @ExceptionHandler({Exception.class})
    public
    @ResponseBody
    String exception(Exception e) {
        return "{" + e.getMessage() + "}";
    }
}
